<template>
  <div id="app">
    <div class="title">
      <span class="text-title">示功图检测系统</span>
    </div>
    <div class="body">
      <div class="login-card">
        <el-card class="box-card">
          <div class="text-login" style="color: #3c3f41">欢 迎 登 陆 示 功 图 检 测 系 统</div>
          <div class="login-body">
            <el-form
                ref="ruleFormRef"
                :model="ruleForm"
                status-icon
                :rules="rules"
                label-width="80px"
                class="demo-ruleForm"
                label-position="left"

            >
              <el-form-item label="用户名" prop="userName" label-class="custom-label" style="color: white">
                <el-input v-model="ruleForm.userName" type="text" autocomplete="off" placeholder="UserName"></el-input>
              </el-form-item>
              <el-form-item label="密码" prop="passWord" label-class="custom-label">
                <el-input
                    v-model="ruleForm.passWord"
                    show-password
                    type="password"
                    autocomplete="off" placeholder="PassWord"></el-input>
              </el-form-item>
              <el-form-item label="验证码" prop="code" label-class="custom-label">
                <el-input v-model="ruleForm.code" class="custom-input"></el-input>
                <div class="img-block">
                  <img class="img-code" @click="refreshCaptcha" :src="captchaImg">
                </div>
              </el-form-item>
            </el-form>
            <div style="color: #606266;font-size: 15px;margin-left: 10px">没有账号点击<span style="color: blue;cursor: pointer" @click="goRegister">注册</span></div>
            <el-form-item style="width: 100%">
              <el-button type="primary" @click="handleLogin('ruleFormRef')"
                         style="margin-top: 35px;width: 100%">LOGIN</el-button>
            </el-form-item>
          </div>
        </el-card>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue';
import axios from 'axios';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';

const captchaImg = ref('');
const ruleForm = reactive({
  userName: '',
  passWord: '',
  code: ''
});

const rules = {
  userName: [{ required: true, message: 'Please enter your username', trigger: 'blur' }],
  passWord: [{ required: true, message: 'Please enter your password', trigger: 'blur' }],
  code: [{ required: true, message: 'Please enter the verification code', trigger: 'blur' }]
};

const router = useRouter();

const handleLogin = (formRef) => {
  const form = formRef.value;
  if (!form) {
    console.error('Form reference is not provided.');
    return;
  }

  form.validate((valid) => {
    if (valid) {
      const requestData = {
        username: ruleForm.userName,
        password: ruleForm.passWord,
        code: ruleForm.code
      };

      axios.post('/user/login', requestData)
          .then((response) => {
            ElMessage.success('登录成功');
            console.log('Login successful', response.data);
            router.push('/home/homePage');
          })
          .catch((error) => {
            ElMessage.error('登录失败');
            console.error('Failed to login', error);
          });
    } else {
      console.log('Login failed. Form validation error.');
      return false;
    }
  });
};

const refreshCaptcha = () => {
  captchaImg.value = '';
  axios.get('/user/testGetCaptcha')
      .then(response => {
        captchaImg.value = response.data;
      })
      .catch(error => {
        console.error('获取验证码失败:', error);
      });
};

onMounted(() => {
  refreshCaptcha();
});

const enter = () => {
  document.addEventListener('keydown', (e) => {
    let key = e.keyCode;
    if (key == 13) {
      handleLogin('ruleFormRef');
    }
  });
};

enter();
</script>
<style scoped lang="scss">
.title{
  height: 80px;
  width: 100%;
  line-height: 80px;
  background-color: #027bff;
}
.text-title{
  display: inline-block;
  font-size: 37px;
  font-weight: 800;
  margin-left: 40px;
  color: white;
}
.body{
  width: 100%;
  height: 716px;
  background-image: url("@/assets/img/0e08e038f042497f089cbfe553e0f278.jpeg");
  background-size: cover;
  background-repeat: no-repeat;
}
.login-card{
  position: relative;
}
.box-card {
  display: flex;
  justify-content: center;
  position: absolute;
  width: 350px;
  height: 420px;
  margin-top: 140px;
  margin-left: 38%;
  border: 2px solid #027bff;
  background: linear-gradient(to right, #fda132, #5479cd)
}
.text-login{
  color: white;
  font-weight: 500;
  font-size: 20px;
  height: 60px;
  line-height: 60px;
  margin-left: 10px;
}
.login-body{
  width: 100%;
  height: 500px;
  margin-top: 50px;
}
.custom-input{
  width: 110px;
}
.img-block{
  display: inline-block;
  width: 100px;
  height: 32px;
  margin-left: 15px;
  /*background-color: white;*/
}
.img-code{
  width: 100%;
  height: 100%;
}
:deep(.custom-label){
  color: white;
}
</style>
